<?php

namespace App\Http\Controllers;

use App\Models\Footprint;
use App\Models\WechatUser;
use Illuminate\Http\Request;

class FootprintController extends Controller
{
    public function browseList()
    {
        $pn = request()->input('pn', config('api.page_num'));
        $userId = request()->input('user_id');
        $list = Footprint::where('user_a', $userId)
            ->select('id', 'user_a', 'user_b','created_at')
            ->with('userb:id,name,head_img,point,score,score_flag')
            ->orderBy('created_at','desc')
            ->paginate($pn);
        return response()->json(['recode' => 1,
            'data' => ['last_page' => $list->lastPage()
                , 'page' => $list->currentPage()
                , 'list' => $list->items()]]);

    }

    public function browse()
    {
        $userA = request()->input('usera');
        $userB = request()->input('userb');
        //检查用户
        $userAInfo = WechatUser::find($userA);
        if (!$userAInfo) {
            return response()->json(['recode' => 0, 'msg' => "用户不存在"]);
        }
        $userBInfo = WechatUser::find($userB);
        if (!$userBInfo) {
            return response()->json(['recode' => 0, 'msg' => "用户不存在"]);
        }
        $footprint = Footprint::where('user_a', $userA)
        ->where('user_b', $userB)
        ->first();
        if ($footprint){
            //进行修改时间
            $footprint->created_at = time();
            $footprint->save();
        }else{
            //新增关注
            $sub = Footprint::firstOrCreate(['user_a' => $userA, 'user_b' => $userB]);
            if ($sub) {
                return response()->json(['recode' => 1, 'msg' => "添加足迹成功"]);
            }
        }
    }

    public function del()
    {
        $userA = request()->input('usera');
        $userB = request()->input('userb');

        //删除足迹
        Footprint::where([['user_a', $userA], ['user_b', $userB]])->delete();

        return response()->json(['recode' => 1, 'msg' => "删除成功"]);
    }
}
